package csdn;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Singlenton{
    private static Singlenton instance = new Singlenton();
    public static Singlenton getInstance(){
        return instance;
    }
    //在此类的外面无法调用构造方法
    private Singlenton(){

    }
}
public class SIngle {
    public static void main1(String[] args) {
        BlockingQueue<Integer> q = new ArrayBlockingQueue<>(1000);
        Thread t1 = new Thread(()->{
            int i = 1;
            while(true){
                try {
                    q.put(i);
                    System.out.println("t1 "+i);
                    i++;
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        Thread t2 = new Thread(()->{

            while(true){
                int i = 0;
                try {
                    i = q.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t2 "+i);
            }
        });
        t1.start();
        t2.start();
    }

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello");
            }
        });
    }
}

class Singletonlazy{
    private static volatile Singletonlazy instance = null;
    private static Object locker = new Object();
    public static Singletonlazy getInstance(){
        if(instance==null){
            synchronized (locker){
                if(instance==null){
                    instance = new Singletonlazy();
                }
            }
        }
        return instance;
    }
    private Singletonlazy(){

    }
}
class MyBlockingQueue{
    private String[] data = null;
    private int head = 0;
    private int tail = 0;
    private int size = 0;
    public MyBlockingQueue(int capacity){
        data = new String[capacity];
    }
    public void put(String s) throws InterruptedException {
        synchronized (this){
            while(size==data.length){
                //满了
                this.wait();
            }
            data[tail++] = s;
            if(tail>=data.length){
                tail = 0;
            }
            size++;
            this.notify();
        }
    }
    public String take() throws InterruptedException {
        String ret = "";
        synchronized (this){
            while (size==0){
                this.wait();
            }
            ret = data[head];
            head++;
            if(head>=data.length){
                head = 0;
            }
            size--;
            this.notify();
        }
        return ret;
    }
}
class MyThreadPool{
    public BlockingQueue<Runnable> queue  = new ArrayBlockingQueue<>(1000);
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(()->{
                while(true){
                    try {
                        Runnable runnable = queue.take();
                        runnable.run();

                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
class MyTimerTask{

}